Ccay.Module.define("Ccay.example.ui.igird.renderFunction",function(page,$S){
	page.ready = function(){

		var gridParam1 = {
			height: 347,
			
		    columns: [
		 		{field: 'name', width:70, header: "string"},
		 		{field: 'sex', width:50, header: "bool", type: 'bool', format:{htmlView: true}},
		 		{field: 'age', width:60, header: "integer", type: 'integer'},
		 		{field: 'tuition', width:70, header: "number", type: 'number', format:{fraction: 2}},
		 		{field: 'tuition', width:75, header: "currency", type: 'currency', format:{fraction: 2, flag: '￥[currency]'}},
		 		{field: 'attr5', width:80, header: "currencystr", type: 'currencystr', format:{fraction: 2, flag: '￥[currencystr]'},showTip:true},
		 		{field: 'tuition', width:90, header: "rate", type: 'rate', format: {total: 1  , fraction: 1, htmlView: true, section: [20, 50, 60]}},
		 		{field: 'birthday', width:80, header: "date", type: 'date', format: {dateFormat:'yyyy-MM-dd'}},
		 		{field: 'lastUpdateDate', width:130, header: "datetime", type: 'datetime', format: {renderFormat: 'MM/dd/yyyy hh:mm'}, content: "${#getLastTime(lastUpdateDate)}"},
		 		{field: 'province', width:70, header: "lookup", type: 'lookup', format: {supportLang: true, code: "province"}},
		 		{field: 'address', width:110, header: "enum", showTip: true, type: 'enum', format: {service: 'FN:Ccay.example.ui.igird.renderFunction.provinceData', cacheKey: "render_provinceData", valueField: "pCode", displayField: "pName"}},
		 		{field: 'blog', width:150, header: "ellipsis", renderer: 'ellipsis', format: {len: 20}},
		 		{field: 'blog', width:150, header: "longtext", type: 'longtext'}
		    ],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/${pageSize}/${curPage}/${orderBy}'
			}
		};
		
		var showBlogProto = {
			getFormat: function (field, format) {
	        	return "<a href='${blog}'>${blog}</a>";
	    	},
	    	format:function(value,format){
	    		if(!format) format = {};
	    		if(value.substr(0,7) != format.flag)
	    			value = format.flag + value;
	    		return "<a href='{0}'>{1}</a>".formatValue(value, value);
	    	},
	    	render: function (target, value, format) {
	        	target.html(this.format(value,format));
	    	}
		};
		
		var showLastDate = {
			getFormat : function(field, format){
				return "${Ccay.Format.toDateBefore({0})}".formatText(field);
			}
		}
		
		getLastTime = function(data){
			return Ccay.Format.toDateBefore(data && data.getUTCTime());
		};
		
		var gridParam2 = {
			height: 347,
			editMode: true,
			wrap: true,
		    columns: [
		    	{field: 'blog', width:170, header: "sBlog(normal)", editorOp: {type: 'textarea'}, renderer: "longtext", format: {height: "50px"}},
		 		{field: 'blog1', width:170, header: "sBlog(render)", editorOp: {type: 'textarea'}, format:{flag:'http://'}, renderer: Ccay.UI.Renderer.create(showBlogProto)},
		 		{field: 'lastUpdateDate', width:170, header: "lastUpdatedDate(normal)", editable: false, type: 'datetime', content: "${#getLastTime(lastUpdateDate)}"},
		 		{field: 'lastUpdateDate', width:170, header: "lastUpdatedDate(render)", editable: false, type: 'datetime', content: "${#getLastTime(lastUpdateDate)}"}
		    ],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/${pageSize}/${curPage}/${orderBy}'
			}
		};
		
		var gridParam3 = {
				editMode: true,
				rowEdit: true,
				showPager: false,
			    columns: [
			 		{field: 'name', width:100, header: "string"},
			 		{field: 'sex', width:70, header: "bool", type: 'bool'},
			 		{field: 'age', width:90, header: "integer", type: 'integer', rule: {range: [16, 80]}},
			 		{field: 'tuition', width:100, header: "currency", type: 'currency', format:{fraction:2, flag:'￥[currency]'}, rule: {range: [-9999999999, 9999999999]}},
			 		{field: 'birthday', width:100, header: "date", type: 'date'},
			 		{field: 'province', width:100, header: "lookup", type: 'lookup', format: {supportLang: true, code: "province"}},
			 		{field: 'blog', width:140, header: "textarea", type: 'textarea'}
			    ],
			    fnRenderRow: function(row, data, i){
			    	var text = this.getRowCell(row, 1).text();
			    	if(text == "111"){
			    		row.css("background-color","#90F880");
			    	}else if(text == "222"){
			    		row.css("background-color","#ff6600");
			    	}else if(text == "333"){
			    		row.css("background-color","green");
			    	}else if(text == "444"){
			    		row.css("background-color","yellow");
			    	}else if(text == "555"){
			    		row.css("background-color","#ffcc66");
			    	}
			    }
			};
		
		var datas=[
		            {name:"111", sex:true, age:21, tuition:null, birthday:null, province:'广东省', blog:null},
		            {name:"222", sex:false, age:23, tuition:null, birthday:null, province:'湖南省', blog:null},
		            {name:"333", sex:true, age:31, tuition:null, birthday:null, province:'河南省', blog:null},
		            {name:"444", sex:true, age:19, tuition:null, birthday:null, province:'四川省', blog:null},
		            {name:"555", sex:false, age:25, tuition:null, birthday:null, province:'山东省', blog:null}
		          ];
		
		$S("#renderFunctionDemo").ccayTab();
		
		//点击tab头后，才加载数据，只第一次生效
		$S("li[tabid='tab2']").one("click", function () {
			$S("#iStudent1").igrid(gridParam1);
		});
		
		$S("li[tabid='tab3']").one("click", function () {
			$S("#iStudent2").igrid(gridParam2);
		});
		
		$S("li[tabid='tab4']").one("click", function () {
			$S("#iStudent3").igrid(gridParam3,datas);
		});
	};
	
	page.provinceData = function(){
		var provinceList = [
			{"pCode": 0, "pName": "广东省"},
			{"pCode": 1, "pName": "广西省"},
			{"pCode": 2, "pName": "湖北省"},
			{"pCode": 3, "pName": "湖南省"},
			{"pCode": 4, "pName": "四川省"},
			{"pCode": 5, "pName": "山东省"},
			{"pCode": 6, "pName": "山西省"}
		];
		return provinceList;
	};
	
	page.openDemo = function(tabId){
		$S("#renderFunctionDemo").getCcayTabManager().selectItem(tabId);
		$S("li[tabid='" + tabId + "']").click();
	};
	
});